clc;clear;clearvars;
% 随机生成20个数据
num_initial = 20;
num_vari = 60;
% 搜索区间
upper_bound = 5.12;
lower_bound = -5.12;
% 迭代时计算y值总的个数
eval_num = 12000;
% K表示划分子空间的个数,sub_num表示每个子空间的维度
K = 5;
sub_num = num_vari / K;
% 算法的迭代次数
iter = eval_num / K;
w = 1;
% 随机生成20个数据,并获得其评估值
sample_x = lhsdesign(num_initial, num_vari).*(upper_bound - lower_bound) + lower_bound.*ones(num_initial, num_vari);
sample_y = Rastrigin(sample_x);
Fmin = zeros(eval_num, 1);
aver_Fmin = zeros(eval_num, 1);

for n = 1 : 100
    n1 = 1;
    % 初始化一些参数
    pbestx = sample_x;
    pbesty = sample_y;
    % 当前位置信息presentx
    presentx = lhsdesign(num_initial, num_vari).*(upper_bound - lower_bound) + lower_bound.*ones(num_initial, num_vari);
    vx = sample_x;
    [fmin, gbest] = min(pbesty);
    global_best_x = pbestx(gbest, :);
    fprintf("n: %.4f\n", n);
    % fprintf("iter 0 fmin: %.4f\n", fmin);
    for i = 1 : iter
        for i1 = 1 : K
            ind = ((1 + (i1 - 1) * sub_num) : i1 * sub_num);
            r = rand(num_initial, sub_num);
            % pso更新下一步的位置,这里可以设置一下超过搜索范围的就设置为边界
            vx(:, ind) = w.*vx(:, ind) + 2 * r .* (pbestx(:, ind) - presentx(:, ind)) + 2 * r .* (pbestx(gbest, ind) - presentx(:, ind));
            vx1 = vx(:, ind);
            vx1(vx1 > upper_bound) = upper_bound;
            vx1(vx1 < lower_bound) = lower_bound;
            vx(:, ind) = vx1;
            presentx(:, ind) = presentx(:, ind) + vx1;
            presentx1 = presentx(:, ind);
            presentx1(presentx1 > upper_bound) = upper_bound;
            presentx1(presentx1 < lower_bound) = lower_bound;
            presentx(:, ind) = presentx1;

            presentx2 = repmat(global_best_x, num_initial, 1);
            presentx2(:, ind) = presentx1;
            presenty = Rastrigin(presentx2);

            % 更新每个单独个体最佳位置
            pbestx1 = pbestx(:, ind);
            pbestx1(presenty < pbesty, :) = presentx1(presenty < pbesty, :);
            pbestx(:, ind) = pbestx1;
            pbesty(presenty < pbesty, :) = presenty(presenty < pbesty, :);
            
            % 更新所有个体最佳位置
            [fmin, gbest] = min(pbesty);
            global_best_x = pbestx(gbest, :);
            Fmin(n1, 1) = fmin;
            n1 = n1 +1;
            % fprintf("iter %d fmin: %.4f\n", i, fmin);
        end
    end
    aver_Fmin = aver_Fmin + Fmin;
end
aver_Fmin = aver_Fmin ./ 100;
plot(aver_Fmin);